//
//  SegmentContainView.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15/7/24.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "SegmentContainView.h"

@interface SegmentContainViewLine : UIView

@end

@implementation SegmentContainViewLine

@end

@implementation SegmentAttribute

@end

@interface SegmentContainView ()<XUIButtonDelegate>
{
    NSMutableArray *_item;
    
    UIView *_line;
    
    NSMutableArray *_viewItem;
}

@end

@implementation SegmentContainView

@synthesize item = _item;

-(id)init
{ 
    self = [super initWithFrame:CGRectMake(0, 0, 0, 48)];
    
    _item = [NSMutableArray new];
    
    _verticalLineColor = [UIColor colorWithHex:0xdedfe0];
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.width = self.superview.width;
    
    if(_viewItem == nil)
    {
        _viewItem = [NSMutableArray new];
        
        BOOL b1 = [_delegate respondsToSelector:@selector(numberOfItemInSegmentContainView:)];
        BOOL b2 = [_delegate respondsToSelector:@selector(segmentContainView:viewForItemAtIndex:)];
        
        NSUInteger count = _item.count;
        
        if(b1 == YES && b2 == YES)
            count = [_delegate numberOfItemInSegmentContainView:self];
        
        for(NSUInteger i = 0; i < count ;i++)
        {
            UIView *v = nil;
            if(b2 == NO)
            {
                SegmentAttribute *item = _item[i];
                XUIButton *btn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCustom delegate:self];
                [btn SetImage:item.headImage];
                [btn SetTitle:item.title];
                btn.szTag = item.tag;
                [btn SetFont:[UIFont systemFontOfSize:16]];
                btn.backgroundColor = item.backgroundColor;
                [btn SetTitleColor:item.titleColor];
                v = btn;
            }
            else
            {
                v = [_delegate segmentContainView:self viewForItemAtIndex:i];
                v.tag = 100000 + i;
                UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
                [v addGestureRecognizer:ges];
            }
            
            [self addSubview:v];
            
            [_viewItem addObject:v];
            
            if(i < count - 1)
            {
                SegmentContainViewLine *line = [[SegmentContainViewLine alloc]initWithFrame:CGRectZero];
                line.backgroundColor = self.verticalLineColor;
                [self addSubview:line];
                
                [_viewItem addObject:line];
            }
        }
        
        UIView *line = self.subviews[0];
        line.width = self.width;
        
        [self bringSubviewToFront:line];
    }
    
    CGFloat w = self.width - (_viewItem.count / 2) * 0.5;
    w = w / ((_viewItem.count + 1) / 2);
    
    for(NSUInteger i = 0; i < _viewItem.count;i++)
    {
        UIView *view = _viewItem[i];
        if([view isMemberOfClass:[SegmentContainViewLine class]])
        {
            view.frame = CGRectMake(w * (i/2 + 1) + 0.5 * (i -1), 0.5, 0.5, self.height - 0.5);
        }
        else
        {
            view.frame = CGRectMake(w * i / 2 + 0.5 * i, 0.5, w, self.height - 0.5);
        }
    }
}

-(CGRect)button:(XUIButton *)btn imageContentRect:(CGRect)rect
{
    SegmentAttribute *item = nil;
    for(item in _item)
    {
        if([item.tag isEqualToString:btn.szTag])
            break;
        else
            item = nil;
    }
    
    CGSize titleSize = [item.title stringSizeWithFont:[UIFont systemFontOfSize:16]];
    CGSize imgaeSize = item.headImage.size;
    
    CGFloat l = (btn.width - (titleSize.width + imgaeSize.width + 10)) / 2;
    
    return CGRectMake(l, (btn.height - imgaeSize.height)/2, imgaeSize.width, imgaeSize.height);
}

-(CGRect)button:(XUIButton *)btn titleContentRect:(CGRect)rect
{
    SegmentAttribute *item = nil;
    for(item in _item)
    {
        if([item.tag isEqualToString:btn.szTag])
            break;
        else
            item = nil;
    }
    
    CGSize titleSize = [item.title stringSizeWithFont:[UIFont systemFontOfSize:16]];
    CGSize imgaeSize = item.headImage.size;
    
    CGFloat l = (btn.width - (titleSize.width + imgaeSize.width + 10)) / 2;
    l += imgaeSize.width + 10;
    
    return CGRectMake(l, 0, titleSize.width, btn.height);
}

-(void)clickedUpInsideOfButton:(XUIButton *)btn
{
    if([_delegate respondsToSelector:@selector(segmentContainView:clickedSegmentWithTag:)])
        [_delegate segmentContainView:self clickedSegmentWithTag:btn.szTag];
}

-(void)tapGestureRecognizer:(UIGestureRecognizer*)ges
{
    if(ges.state == UIGestureRecognizerStateEnded)
    {
        if([_delegate respondsToSelector:@selector(segmentContainView:clickedSegmentAtIndex:)])
            [_delegate segmentContainView:self clickedSegmentAtIndex:ges.view.tag - 100000];
    }
}

-(void)reloadItem:(SegmentAttribute*)item
{
    for(NSUInteger i = 0;i < _item.count ;i++)
    {
        SegmentAttribute *it = _item[i];
        if([it.tag isEqualToString:item.tag])
        {
            [_item replaceObjectAtIndex:i withObject:item];
            break;
        }
    }
    
    [_viewItem makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _viewItem = nil;
    [self setNeedsDisplay];
}

-(void)reloadAtIndex:(NSUInteger)index
{
    if([_delegate respondsToSelector:@selector(segmentContainView:viewForItemAtIndex:)])
    {
        UIView *v = [_delegate segmentContainView:self viewForItemAtIndex:index];
        UIView *old = _viewItem[index];
        
        v.tag = old.tag;
        
        [self insertSubview:v aboveSubview:old];
        
        [_viewItem replaceObjectAtIndex:index withObject:v];
        
        [old removeFromSuperview];
        
        [self setNeedsDisplay];
    }
}

-(void)reloadItem
{
    
}

@end
